package org.noote.libs.sogle.helper;

import org.noote.libs.sogle.LineMesh;

public class LineMesh_Grid extends LineMesh {

	public LineMesh_Grid(float width, float height, int widthSegments,
			int heightSegments) {
		float[] vertices = new float[((widthSegments + 1) + (heightSegments + 1))
		             				* 3 * 2];
		float[] normales = new float[vertices.length];
		short[] indices = new short[((widthSegments + 1) + (heightSegments + 1))
				* 2];

		float xOffset = width / -2;
		float zOffset = height / -2;
		float xWidth = width / (widthSegments);
		float zHeight = height / (heightSegments);
		int currentVertex = 0;
		int currentIndex = 0;
		for (int z = 0; z < heightSegments + 1; z++)
		{
			// points
			vertices[currentVertex] = xOffset;
			vertices[currentVertex + 1] = 0;
			vertices[currentVertex + 2] = zOffset + z * zHeight;
			normales[currentVertex] = 0;
			normales[currentVertex + 1] = 1;
			normales[currentVertex + 2] = 0;
			currentVertex += 3;
			
			vertices[currentVertex] = xOffset + width;
			vertices[currentVertex + 1] = 0;
			vertices[currentVertex + 2] = zOffset + z * zHeight;
			normales[currentVertex] = 0;
			normales[currentVertex + 1] = 1;
			normales[currentVertex + 2] = 0;
			currentVertex += 3;
			
			// line
			indices[currentIndex] = (short) currentIndex;
			indices[currentIndex+1] = (short)(currentIndex+1);
			currentIndex +=2;
		}
		for (int x = 0; x < widthSegments + 1; x++)
		{
			// points
			vertices[currentVertex] = xOffset + x * xWidth;
			vertices[currentVertex + 1] = 0;
			vertices[currentVertex + 2] = zOffset;
			normales[currentVertex] = 0;
			normales[currentVertex + 1] = 1;
			normales[currentVertex + 2] = 0;
			currentVertex += 3;
			
			vertices[currentVertex] = xOffset + x * xWidth;
			vertices[currentVertex + 1] = 0;
			vertices[currentVertex + 2] = zOffset + height;
			normales[currentVertex] = 0;
			normales[currentVertex + 1] = 1;
			normales[currentVertex + 2] = 0;
			currentVertex += 3;
			
			// line
			indices[currentIndex] = (short) currentIndex;
			indices[currentIndex+1] = (short)(currentIndex+1);
			currentIndex +=2;
		}

		setIndices(indices);
		setVertices(vertices);
		setVerticeNormales(normales);
		
		//matEnableMaterial = false;
	}
}
